package net.jdm.constraint.distance;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.jdm.common.Alert;
import net.jdm.common.Alert.AlertLevel;
import net.jdm.common.Alert.DetailLevel;
import net.jdm.constraint.Constraint;
import net.jdm.domain.entity.ContainerNode;


public class DistanceConstraint  implements Constraint{
protected DistancePrinter distancePrinter=DistancePrinter.getInstance();
private static final Log LOG = LogFactory.getLog(DistanceConstraint.class);
   private double thresholdValue;
   private Alert alert=new Alert();
	public DistanceConstraint(double thresholdValue){
	       this.thresholdValue=thresholdValue;
	}
	public boolean check(ContainerNode containerNode) {
		if(containerNode.isStable())
			return true;
		double distance=containerNode.distance();
		boolean pass=distance<=getThresholdValue();
		if(!pass||LOG.isDebugEnabled()){
			distancePrinter.printDistanceInfo(containerNode,getAlert(), getThresholdValue());
		}
		return pass;
	}
	public double getThresholdValue() {
		return thresholdValue;
	}
	public void setThresholdValue(double thresholdValue) {
		this.thresholdValue = thresholdValue;
	}
	@Override
	public void setAlertLevel(AlertLevel alertLevel) {
		alert.setAlertLevel(alertLevel);
	}
	@Override
	public void setDetailLevel(DetailLevel detailLevel) {
		this.alert.setDetailLevel(detailLevel);
	}
	@Override
	public void setAlertMessage(String alertMessage) {
		this.alert.setAlertMessage(alertMessage);
	}
	public Alert getAlert() {
	    return alert;
	}

}
